﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Dropthings.Widget.Widgets;
using Dropthings.Widget.Framework;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;

public partial class Widgets_SLWCFPowerShellOutputWidget : System.Web.UI.UserControl, IWidget
{
    string strResult;
    public string ColumnValue = "";
    public string DataValue = "";

    private IWidgetHost _Host;

    private XElement _State;
    private XElement State
    {
        get
        {
            string state = this._Host.GetState();
            if (string.IsNullOrEmpty(state))
                state = "<state><strPoshCode></strPoshCode><strPoshHeight>300</strPoshHeight><strHtmlRender></strHtmlRender><refresh>false</refresh></state>";
            if (_State == null) _State = XElement.Parse(state);
            return _State;
        }
    }

    public bool Refresh
    {
        get { return bool.Parse((State.Element("refresh") ?? new XElement("refresh", "false")).Value); }
        set
        {
            if (State.Element("refresh") != null) State.Element("refresh").Value = value.ToString();
            else State.Add(new XElement("refresh", value.ToString()));
        }
    }

    public string strPoshCode
    {
        get { return (State.Element("strPoshCode") ?? new XElement("strPoshCode", "")).Value; }
        set { State.Element("strPoshCode").Value = value; }
    }

    public string strPoshHeight
    {
        get { return (State.Element("strPoshHeight") ?? new XElement("strPoshHeight", "")).Value; }
        set { State.Element("strPoshHeight").Value = value; }
    }

    public string strHtmlRender
    {
        get { return (State.Element("strHtmlRender") ?? new XElement("strHtmlRender", "")).Value; }
        set { State.Element("strHtmlRender").Value = value; } 
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        string strArgedCode = strPoshCode;
        if (Session["Refresher"] != null)
        {
            string strTraceSession = Session["Refresher"].ToString();
            string[] strSplittedArgs = strTraceSession.Split(';');
            strArgedCode = strPoshCode.Replace(strSplittedArgs[0], strSplittedArgs[1]);
        }
    }

    void IWidget.Init(IWidgetHost host)
    {
        this._Host = host;
    }

    void IWidget.ShowSettings()
    {
        PoshtextBox.Text = this.strPoshCode;
        PoshHeight.Text = this.strPoshHeight;
        SettingsPanel.Visible = true;
    }
    void IWidget.HideSettings()
    {
        SettingsPanel.Visible = false;
        Page.Response.Redirect(Page.Request.Url.ToString(), true);
    }
    void IWidget.Minimized()
    {
    }
    void IWidget.Maximized()
    {
    }
    void IWidget.Closed()
    {
    }


    protected void executePowerShellCode(string code)
    {
        //StringWriter writer = new StringWriter();
        //Server.HtmlDecode(code, writer);
        //StreamReader objReader = new StreamReader(Server.MapPath("~/PowerShell/ConvertChart.ps1"));
        //string strContent = objReader.ReadToEnd();
        //objReader.Close();
        //string strPoshCode = strContent + writer.ToString();
        //// create Powershell runspace
        //Runspace runspace = RunspaceFactory.CreateRunspace();
        //runspace.Open();
        //Pipeline pipeline = runspace.CreatePipeline();
        //pipeline.Commands.AddScript(strPoshCode);
        //pipeline.Commands.Add("Out-String");
        //Collection<PSObject> results = pipeline.Invoke();
        //runspace.Close();

        //// convert the script result into a single string
        //StringBuilder stringBuilder = new StringBuilder();
        //foreach (PSObject obj in results)
        //{
        //    stringBuilder.AppendLine(obj.ToString());
        //}
        //string strDataInput = stringBuilder.ToString();
        StreamReader htmlBlackLight = new StreamReader(Server.MapPath("~/PowerShell/SLWCFPowerShellOutput.html"));
        string targetHtml = htmlBlackLight.ReadToEnd();
        strResult = targetHtml;        
    }

    private void SaveState()
    {
        var xml = this.State.Xml();
        this._Host.SaveState(xml);
    }

    protected void SaveSettings_Clicked(object sender, EventArgs e)
    {
        string PoshCode = PoshtextBox.Text;
        this.strPoshCode = PoshCode;
        this.strPoshHeight = PoshHeight.Text;
        this.executePowerShellCode(PoshCode);
        this.strHtmlRender = strResult;
        this.State.Add(new XCData(strResult));
        this.SaveState();
        Page.Response.Redirect(Page.Request.Url.ToString(), true);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        StreamReader htmlBlackLight = new StreamReader(Server.MapPath("~/PowerShell/SLWCFPowerShellOutput.html"));
        string targetHtml = htmlBlackLight.ReadToEnd();
        this.Output.Text = targetHtml;
    }
    protected void nbrCharts_changed(object sender, EventArgs e)
    {

    }
}
